<?php


namespace App\Http\Controllers\Merchant;
use App\Http\Controllers\BaseController;
use App\Models\Cart;
use App\Models\Category;
use App\Models\MallProductStock;
use App\Models\Product;
use App\Models\Tag;
use App\Services\MallOrderService;
use Illuminate\Http\Request;

class ProductController extends BaseController
{
    public $model = Product::class;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $search = ['title' => '', 'category_id' => 0, 'on_status' => 1
            , 'start_at' => null, 'end_at' => null];
        $input = $request->input();
        $product_no = $input['product_no'] ?? null;
        $amount_min = $input['amount_min'] ?? null;
        $amount_max = $input['amount_max'] ?? null;
        $order_by_key = $request->input('order_by_key', 'id');
        $order_by_value = $request->input('order_by_value', 'desc');

        $list = Product::apply($search, $input)->when($amount_min, function ($q) use($amount_min){
            $q->where('amount', '>=', $amount_min * 100);
        })->when($amount_max, function ($q) use($amount_max){
            $q->where('amount', '<=', $amount_max * 100);
        })->when($product_no, function ($q) use($product_no){
            $q->where('product_no', 'like', '%' . $product_no . '%');
        })->withCertain('category', ['title'])->withCertain('parent_category', ['title'])->orderBy($order_by_key, $order_by_value)
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }

}

